﻿//==================================================================
// Copyright (C) 2011 iSun Studio 软件工作室
// 作 者：张骁晓
// Email:iSunStudio@hotmail.com
// 日 期：2011/12/02
// 描 述：常用反射方法,用于动态创建实体类，以及动态获取/设置实体对象的属性值
// 版 本：1.00
// 
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// V1.01  2011/10/28   贺兴祥            功能菜单选项      
//    
//==================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iSun.BLL.Base;
using System.Web.UI.WebControls;
using iSun.Entity;
using System.ComponentModel;
namespace iSun.Web.UI.Controls.UserControl
{
    public class DeptList : Literal
    {
        IList<DatDepartment> list = null;
        IList<DatDepartment> rootlist = null;
        string sql = "";
        private bool _isLoad = true;
        [Description("是否加载")]
        public bool IsLoad
        {
            get { return _isLoad; }
            set { _isLoad = value; }
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (!DesignMode && IsLoad)
            {
                sql = "select deptId,ParentID,deptName,deptPath,isoutdepartment from datdepartment where (isvirtualdepartment=0 or isvirtualdepartment is null)  and (isoutdepartment=0 or isoutdepartment is null) ";
                list = new EntityBLL().GetEntitysBySqlComm<DatDepartment>(sql);
                rootlist = list.Where(p => p.DeptPath.Split('-').Length == 1).OrderBy(p => p.DeptPath).ToList();
                StringBuilder _strHtml = new StringBuilder();
                _strHtml.Append("<div>");
                _strHtml.Append("<Select id='SelectForDept' onchange='ChangeDept();' style='padding-bottom:2px'>");
                int i = 0;
                foreach (DatDepartment tree in rootlist)
                {
                    if (i == 0)
                    {
                        _strHtml.Append("<option value=" + tree.ID.ToString() + " selected='selected'>");
                        i++;
                    }
                    else
                    {
                        _strHtml.Append("<option value=" + tree.ID.ToString() + ">");
                    }
                    _strHtml.Append(tree.Name);
                    _strHtml.Append("</option>");
                    AddNode(tree.ID, _strHtml, list);
                }
                _strHtml.Append("</Select>");
                _strHtml.Append("</div>");
                writer.Write(_strHtml);
                base.Render(writer);

            }
           
        }
        private void AddNode(Guid deptId, StringBuilder _strHtml, IList<DatDepartment> list)
        {
            var detail = list.Where(p => p.ParentID == deptId).OrderBy(p => p.DeptPath).ToList();
            if (detail.ToList().Count > 0)
            {
                foreach (DatDepartment v in detail)
                {
                    _strHtml.Append("<option value="+v.ID.ToString()+">");
                    int length = v.DeptPath.Split('-').Length;
                    string flag = "";
                    for (int i = 1; i < length; i++)
                    {
                        flag += "-";
                        _strHtml.Append(flag);
                    }
                    _strHtml.Append(v.Name);
                    _strHtml.Append("</option>");
                    AddNode(v.ID, _strHtml, list);

                }
                _strHtml.Append("</option>");
            }
        }


    }
}
